package com.apex.sdk.ui;

import android.Manifest;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatButton;
import androidx.appcompat.widget.Toolbar;
import androidx.core.app.ActivityCompat;

import com.apex.sdk.R;
import com.apex.sdk.databinding.ActivityStartPageBinding;
import com.apex.sdk.utils.SharedData;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.IllegalFormatCodePointException;
import java.util.List;


public class StartPageActivity extends AppCompatActivity {
    private final int CODE_PERMISSION = 0x22;
    final String TAG = this.getClass().getName();

    final String deviceMacAddress = "45:41:06:D7:56:D4";
    // final String deviceMacAddress = "45:41:11:73:67:6F";
    ActivityStartPageBinding startPageBinding;
    long clickTime;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        startPageBinding = ActivityStartPageBinding.inflate(getLayoutInflater());
        setContentView(startPageBinding.getRoot());
        startPageBinding.tool.setNavigationIcon(R.mipmap.exit_page);
        setSupportActionBar(startPageBinding.tool);
        getSupportActionBar().setTitle(null);
        startPageBinding.inputMac.setText(SharedData.getMacAddress(StartPageActivity.this));

        startPageBinding.tool.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        List<String> permission = new ArrayList<>();
        permission.add(Manifest.permission.ACCESS_COARSE_LOCATION);
        permission.add(Manifest.permission.ACCESS_FINE_LOCATION);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            permission.add(Manifest.permission.BLUETOOTH_SCAN);
            permission.add(Manifest.permission.BLUETOOTH_CONNECT);
        }
        //  permission.add(Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
        //   permission.add(Manifest.permission.READ_EXTERNAL_STORAGE);



        startPageBinding.directConnection.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                long currentTime = System.currentTimeMillis();
                if (Math.abs(currentTime - clickTime) < 1000) {
                    return;
                }
                clickTime = currentTime;
                String macAddress = startPageBinding.inputMac.getText().toString();
                if (TextUtils.isEmpty(macAddress) || !BluetoothAdapter.checkBluetoothAddress(macAddress.toUpperCase())) {
                    Toast.makeText(StartPageActivity.this, getString(R.string.input_mac), Toast.LENGTH_SHORT).show();
                    return;
                }
                SharedData.saveMacAddress(StartPageActivity.this, macAddress.toUpperCase());
                Intent intent = new Intent(StartPageActivity.this, HomeActivity.class);
                intent.putExtra("blue_address", macAddress.toUpperCase());
                intent.putExtra("checked", startPageBinding.checked.isChecked());
                intent.putExtra("resetWatch", startPageBinding.resetChecked.isChecked());
                startActivity(intent);
                finish();
            }
        });
        startPageBinding.scanBluetooth.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(StartPageActivity.this, MainActivity.class);
                intent.putExtra("checked", startPageBinding.checked.isChecked());
                intent.putExtra("resetWatch", startPageBinding.resetChecked.isChecked());
                startActivity(intent);
            }
        });
        startPageBinding.checkBle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startPageBinding.resetChecked.setChecked(false);
                boolean isChecked = startPageBinding.checked.isChecked();
                if (isChecked) {
                    startPageBinding.checked.setChecked(false);
                } else {
                    startPageBinding.checked.setChecked(true);
                }
            }
        });
        startPageBinding.checkReset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startPageBinding.checked.setChecked(false);
                boolean isChecked = startPageBinding.resetChecked.isChecked();
                if (isChecked) {
                    startPageBinding.resetChecked.setChecked(false);
                } else {
                    startPageBinding.resetChecked.setChecked(true);
                }
            }
        });
        requestPermissions(permission.toArray(new String[0]), CODE_PERMISSION);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == CODE_PERMISSION) {
            for (String permission : permissions) {
                Log.i(TAG, "当前权限：" + permission + ",授权结果：" + (checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED ? "true" : "false"));
            }
            for (int permission : grantResults) {

                if (permission != PackageManager.PERMISSION_GRANTED) {
                    Toast.makeText(this, getString(R.string.location_permission), Toast.LENGTH_SHORT).show();
                    return;
                }
            }
        }

        startPageBinding.directConnection.setVisibility(View.VISIBLE);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
